<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class Product extends Model{
    protected $fillable = [
        'title','description','image','on_sale','rating','sold_count','review_count','price'
    ];

    // 与商品sku 关联
    public function skus(){
        return $this->hasMany(ProductSku::class);
    }

    public function getImageUrlAttribute(){
        // 如果image 字段本身就已经是完整的url 就直接返回
        if(Str::startsWith($this->attributes['image'],['http://','https://'])){
            return $this->attributes['image'];
        }
        return \Storage::disk('public')->url($this->attributes['image']);
    }
}
